package com.mcptt.http.auth.process.passwd.result;

import com.cloudptt.api.product.log.Log;
import com.mcptt.http.auth.process.result.AuthResult;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;


public class FirstPostResult extends AuthResult
{
	private static final String Err_Key = "error=";
	private String cookie = "";
	private URI location = null;

	private String code = "";
	private String state = "";
	private String session_state = "";
	private String error = null;
	private String error_description = null;

	Map<String, String> valueMap = new HashMap<String, String>();

	public FirstPostResult(int result, String desc)
	{
		super(result,desc);
	}

	public String toString()
	{
		StringBuffer strBuf = new StringBuffer("FirstPostResult  ");
		strBuf.append("result=").append(result).append(",");
		strBuf.append("cookie=").append(cookie).append(",");
		strBuf.append("location=").append(location).append(";");

		return strBuf.toString();
	}
	public String getErrStr()
	{
		StringBuffer strBuf = new StringBuffer("FirstGetResult  ");
		strBuf.append("result=").append(result);
		if(error != null
				&& error.length() >0)
		{
			strBuf.append(",");
			strBuf.append("error=").append(error);
			if(error_description != null
					&& error_description.length() >0)
			{
				strBuf.append(",");
				strBuf.append("error_description=").append(error_description).append(";");
			}
			else
			{
				strBuf.append(";");
			}
		}
		else
		{
			strBuf.append(";");
		}

		return strBuf.toString();
	}

	public boolean isSuc()
	{
		if(result == 302
				&& (error == null
				|| error.length() <= 0))
		{
			return true;
		}
		return false;
	}

	public boolean checkReturnParamOk()
	{
//		if(cookie == null
//				|| cookie.length() <= 0
//				|| location == null)
//		{
//			return false;
//		}
		if(location == null)
		{
			return false;
		}
		return true;
	}

	public String getCookie()
	{
		return cookie;
	}

	public void setCookie(String cookie)
	{
		this.cookie = cookie;
	}

	public String getCode()
	{
		return code;
	}

	public String getState()
	{
		return state;
	}

	public String getLocation()
	{
		if(location != null)
		{
			if(location.isAbsolute())
			{
				return location.getPath();
			}
			return location.toString();
		}
		return "";
	}

	public void setLocation(String location)
	{
		if(location != null)
		{
			parse(location);

			code = getValue("code");
			state = getValue("state");
			session_state = getValue("session_state");
			error = getValue("error");
			error_description = getValue("error_description");
			Log.debug(getClass(), "doFirstPost code = " + code + " state = " + state + " session_state = " + session_state + " error = " + error + " error_description = " + error_description );
		}

		try
		{
			this.location = new URI(location);

		} catch (URISyntaxException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void parse(String authStr)
	{
		if(authStr == null
				|| authStr.length() <= 0)
		{
			return;
		}
		authStr = authStr.trim();

		int index = authStr.indexOf('?');
		if(index != -1)
		{
			authStr = authStr.substring(index + 1, authStr.length());
		}

		String[] strArr = authStr.split("&");
		for (int i = 0; i < strArr.length; i++)
		{
			String value = strArr[i];
			String[] valueArr = value.split("=");
			if (valueArr.length == 2)
			{
				if (valueArr[0] != null && valueArr[1] != null)
				{
					String valueStr = valueArr[1].trim();
					valueMap.put(valueArr[0].trim(), valueStr);
				}
			}
		}
	}
	private String getValue(String key)
	{
		if (key == null || key.equals(""))
		{
			return "";
		}
		if (valueMap == null || valueMap.size() <= 0)
		{
			return "";
		}

		String value = valueMap.get(key);
		if (value != null)
		{
			return value;
		}

		return "";
	}
}
